package com.app.web.servlets;

import com.app.common.AppException;
import com.app.common.ResponseResult;
import com.app.pojo.Staff;
import com.app.service.IStaffService;
import com.app.service.impl.StaffService;
import com.app.utils.UtilTools;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Copyright (C) @2020 fuguo.wang@logicsolutions.com.cn
 *
 * @author fuguo.wang
 * @version 1.0
 * @className StaffServlet
 * @date 2020-06-17 21:56
 * @description 员工servlet (员工前端控制层)
 */

@WebServlet(name = "员工控制层", urlPatterns = "/staffs")
public class StaffServlet extends HttpServlet {
	private static final long serialVersionUID = 2811916475703933220L;

	private final IStaffService service = new StaffService();


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		switch (request.getParameter("type")) {
			case "view":
			case "edit":
				String id = request.getParameter("id");
				try {
					Staff staff = service.get(Integer.parseInt(id));
					// 操作成功
					ResponseResult.response(request, response, staff, "staffs/detail.jsp");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 3f, "staffs?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
			/**
			 * 请求删除
			 */
			case "delete":
				// 获取数据主键
				id = request.getParameter("id");
				try {
					// service 交互  去删除
					service.delete(id);
					ResponseResult.info(request, response, "操作提示！", "删除成功！", 1f, "staffs?type=list");
				} catch (AppException e) {
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 2f, "staffs?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
			default: // 默认查询全部用户
				try {
					List<Staff> staffs = service.get();
					// 操作成功
					ResponseResult.response(request, response, staffs, "staffs/list.jsp");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 3f, "index.jsp");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
		}

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		switch (request.getParameter("type")) {
			case "new":
				try {
					Staff staff = this.mapperObj(request);
					service.add(staff);
					ResponseResult.info(request, response, "操作提示！", "新增成功！", 1.8f, "staffs?type=list");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 3f, "staffs?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
			case "edit":
				try {
					Staff staff = this.mapperObj(request);
					service.update(staff);
					ResponseResult.info(request, response, "操作提示", "职员信息更新成功", 1.5f, "staffs?type=list");
				} catch (AppException e) {
					// 提示操作失败
					ResponseResult.warn(request, response, "操作失败！", e.getMessage(), 3f, "staffs?type=list");
				} catch (Exception e) {
					e.printStackTrace();
					// 系统异常
					ResponseResult.error(request, response, null, e, 2.5f, "index.jsp");
				}
				break;
		}
	}


	/**
	 * 从request中获取参数
	 *
	 * @param request
	 * @return
	 */
	private Staff mapperObj(HttpServletRequest request) {
		String id = request.getParameter("id");
		String disable = request.getParameter("disable");
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String deptId = request.getParameter("dept_id");
		String idCard = request.getParameter("id_card");
		String gender = request.getParameter("gender");
		String birthDate = request.getParameter("birth_date");
		String age = request.getParameter("age");
		String nation = request.getParameter("nation");
		String politicalStatus = request.getParameter("political_status");
		String maritalStatus = request.getParameter("marital_status");
		String educationLevel = request.getParameter("education_level");
		String tel = request.getParameter("tel");

		try {
			return new Staff()
					.setCode(code)
					.setName(name)
					.setDeptId(UtilTools.convert(deptId, Integer.class))
					.setIdCard(idCard)
					.setGender(UtilTools.convert(gender, Integer.class))
					.setBirthDate(UtilTools.convert(birthDate, LocalDateTime.class))
					.setAge(UtilTools.convert(age, Integer.class))
					.setNation(UtilTools.convert(nation, Integer.class))
					.setPoliticalStatus(UtilTools.convert(politicalStatus, Integer.class))
					.setMaritalStatus(UtilTools.convert(maritalStatus, Integer.class))
					.setEducationLevel(UtilTools.convert(educationLevel, Integer.class))
					.setTel(tel)
					.setId(UtilTools.convert(id, Integer.class))
					.setDisable("1".equals(disable));
		} catch (NumberFormatException | ClassCastException e) {
			e.printStackTrace();
			throw new AppException(e);
		}
	}

}
